package com.serkol.ams.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.serkol.ams.bean.MyPage;
import com.serkol.ams.bean.MyPageOld;
import com.serkol.ams.entity.AdvertisingManagement;
import com.serkol.ams.mapper.AdvertisingManagementMapper;
import com.serkol.ams.service.AdvertisingManagementService;
import com.serkol.annotation.RedisCache;
import com.serkol.handler.MyLambdaQueryWrapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author pingan
 * @description 针对表【advertising_management】的数据库操作Service实现
 * @createDate 2022-10-28 10:58:41
 */
@Service
public class AdvertisingManagementServiceImpl extends ServiceImpl<AdvertisingManagementMapper, AdvertisingManagement>
        implements AdvertisingManagementService {

    @Override
    public MyPage<Map<String, Object>> findByBackList(int pageSize, int current, int state) {
        MyLambdaQueryWrapper<AdvertisingManagement> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.picture(AdvertisingManagement::getImageId).as("image");
        queryWrapper.select(AdvertisingManagement::getId, AdvertisingManagement::getCreateDate, AdvertisingManagement::getImageId, AdvertisingManagement::getRemark, AdvertisingManagement::getState, AdvertisingManagement::getTitle, AdvertisingManagement::getType, AdvertisingManagement::getTargetType, AdvertisingManagement::getAppid);

        if (state > -1) {
            queryWrapper.eq(AdvertisingManagement::getState, state);
        }

        Page<Map<String, Object>> page = new Page<>(current, pageSize);
        Page<Map<String, Object>> mapPage = this.baseMapper.selectMapsPage(page, queryWrapper);
        return MyPage.parsePage(mapPage);
    }

    @Override
    @RedisCache(key = "advertisingManagement::menu::findByMiniList", isLang = true, isParam = true)
    public List<Map<String, Object>> findByMiniList(int type) {
        MyLambdaQueryWrapper<AdvertisingManagement> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.picture(AdvertisingManagement::getImageId).as("image");
        queryWrapper.select(AdvertisingManagement::getId, AdvertisingManagement::getCreateDate, AdvertisingManagement::getTitle, AdvertisingManagement::getPath, AdvertisingManagement::getType, AdvertisingManagement::getAppid).select(AdvertisingManagement::getTargetType).as("targetType");
        if (type > 0) {
            queryWrapper.eq(AdvertisingManagement::getType, type);
        }

        List<Map<String, Object>> maps = this.listMaps(queryWrapper);
        return maps;
    }

    @Override
    @RedisCache(key = "advertisingManagement::menu::findByMiniByType", isLang = true, isParam = true)
    public Map<String, Object> findByMiniByType(int type) {
        MyLambdaQueryWrapper<AdvertisingManagement> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.picture(AdvertisingManagement::getImageId).as("image");
        queryWrapper.select(AdvertisingManagement::getId, AdvertisingManagement::getCreateDate, AdvertisingManagement::getTitle, AdvertisingManagement::getPath, AdvertisingManagement::getType, AdvertisingManagement::getAppid).select(AdvertisingManagement::getTargetType).as("targetType");
        if (type > 0) {
            queryWrapper.eq(AdvertisingManagement::getType, type);
        }
        queryWrapper.last("LIMIT 1");
        return this.getMap(queryWrapper);
    }

    @Override
    public boolean byDelete(long id) {
        if (id > 0) {
            return this.removeById(id);
        }
        return false;
    }
}




